# -*- coding: utf-8 -*-
"""
Created on Sat Jun  9 19:14:49 2018

@author: Administrator
"""
import os
import glob
import shutil
from ..origin import Shell_script,Matlab
        
#%%
class Standardize(Shell_script,Matlab):
    standardize_method = ''  #子类会自己给standardize_method赋值。不同的值，代表不同的standardize方法
    def initialize_input_list(self,input_list):
        #test if the input_list has 3 parameters
        ##########################################################################重新整理传入参数
        if len(input_list) != 3:
            raise Exception("incompatible parameter amount ") 
        #caveat:self.previous_filename or self.present_filename is actually filepath,in the future version this should be fixed.
        self.previous_dirpath = input_list[0]
        self.mask_filepath = input_list[1]
        self.present_dirpath = input_list[2]
    
    def concat_inputs(self):
        #特有的文件路径
        ImgCells_str = r'ImgCells{1}='
        MaskData_str = r'MaskData='
        MethodType_str = r'MethodType='
        OutputDir_str = r'OutputDir='
        Suffix_str = r"Suffix='';"
        y_Standardization = r'y_Standardization(ImgCells, MaskData, MethodType, OutputDir, Suffix);'
        # "ImgCells= ;MaskData= ;MethodType= ;OutputDir= ;Suffix='';y_Standardization(ImgCells, MaskData, MethodType, OutputDir, Suffix);"
        
        #首先将文件复制的self.present_dirpath，然后使用其中的文件进程处理。y_Standardization函数会生成与self.present_dirpath完全一致的文件夹，将其覆盖掉。
        shutil.copytree(self.previous_dirpath,self.present_dirpath)
        filepaths = glob.glob(self.present_dirpath + self.os_sep + self.star)
        #下面这一步是为了对于filepaths中的每一个元素生成 medi{1}='filepath';
        decorate_filepaths = ['medi'+ self.l_brace_quation + str(filepaths.index(i)+1) + self.r_brace_quation + self.equal_mark + self.s_quation + i + self.s_quation + self.semicolon for i in filepaths]
        #下面是将decorate_filepaths所有的元素都连在一起
        all_filepath = ''.join(decorate_filepaths)
        #先构建medi这个cell，然后再用ImgCell{1} = medi赋值给ImgCell{1}
        #下面开始构建y_Standardization这个函数的各个参数
        ImgCells = all_filepath + ImgCells_str + "medi';"
        MaskData = MaskData_str + self.s_quation + self.mask_filepath + self.s_quation + self.semicolon
        MethodType = MethodType_str + self.standardize_method + self.semicolon
        #os.path.split(self.present_dirpath)[0]，之所以这个，是因为y_Standardization这个函数的特殊性导致的
        OutputDir = OutputDir_str  + self.s_quation + os.path.split(self.present_dirpath)[0]  + self.s_quation + self.semicolon
        Suffix = Suffix_str
        self.standardize_script = self.quation + ImgCells + MaskData + MethodType + OutputDir + Suffix + y_Standardization + self.quation
        print('self.standardize_script:',self.standardize_script)

    def concat_script(self):
        
        #cmd_1的内容：cp self.previous_filename + self.present_filename  #为了在matlab上测试，先改为window的copy命令
#       self.cmd_1 = r'cp -r ' + self.previous_dirpath + self.space  + self.present_dirpath
        
        #cmd_2的内容：matlab -nojvm -nodesktop -nodisplay -nojvm -r  reslice_script
        self.cmd_2 = self.matlab_cmd_str  + self.standardize_script
        
#       self.shell_script = self.cmd_1 + self.cmd_sep + self.cmd_2
        
        self.shell_script = self.cmd_2
#%%
class Mean_regression(Standardize):
    standardize_method = '1'

#%%
class Z_standardize(Standardize):
    standardize_method = '4'

#%%
class Mean_division(Standardize):
    standardize_method = '5'

#
#%% Z_standardize
    
#from agilepipe.process import Z_standardize
#
#previous_dirpath = 
#mask_filepath = 
#present_dirpath = 
#
#Z_standardize([previous_dirpath,mask_filepath,present_dirpath])
    
    
#%% mean division

#previous_dirpath = r'/media/root/Elements3/standardize_test/CBF_clear_background_SN_SR_SSM-6'
#mask_filepath = r'/media/root/Elements3/XuanWu_z_sm666_pearson_0609/CBF_processing/make_mask_before_z_standardize/sws_CBF_26_intersect_mask.nii'
#present_dirpath = r'/media/root/Elements3/standardize_test/CBF_clear_background_SN_SR_SSM-6_md'
#
#Mean_division([previous_dirpath,mask_filepath,present_dirpath])